<?php
class Form {

	var $action = "";
	var $attr;
	var $default_width = 300;
	var $default_height = 100;
	var $data = array();
	var $row = array();

	function Form($attr){
		$this->attr = $attr;
	}

	function add_data($data) {
		$this->data = $data;
    }

	function compile_form(){

		foreach($this->attr as $k => $v){

			$tmp = array();
			$tmp['key'] = $k;
			$tmp['type'] = $v['type'];
			$tmp['style'] = $v['style'];
			$tmp['desc'] = $v['desc'];
			$tmp['show'] = $v['show'];

			$tmp['title'] = ($v['title'])?$v['title']:ucfirst($k);
			$tmp['width'] = ($v['width'])?$v['width']:$this->default_width;
			$tmp['height'] = ($v['height'])?$v['height']:$this->default_height;

			if($v['edit'] === false){
				$v['edit'] = "readonly='readonly'";
			}

			switch ($v['type']) {
				case "hidden" : 
					$tmp['form'] = "<input type = 'hidden' name = '$k' value = '".$v['data']."'>\n";
					break;

				case "text" : 
					$tmp['form'] = "<input type = 'text' id = '$k' class = 'input w".$tmp['width']."' name = '$k' ".$v['edit']." value = '".$v['data']."'>\n";
					break;

				case "password" : 
					$tmp['form'] = "<input type = 'password' id = '$k' class = 'input w".$tmp['width']."' name = '$k' ".$v['edit']." value = '".$v['data']."'>\n";
					break;

				case "upload" :
					$tmp['form'] = "<input type = 'file' name = '$k'>\n";
					break;

				case "textarea" :
					if($v['RTE'] == true){
						$rte = "mce";
					}

					$tmp['form'] = "<textarea class = '".$rte." w".$tmp['width']." h".$tmp['height']."' name = '".$k."' ".$v['edit'].">".$v['data']."</textarea>\n";
					break;

				case "date" : 
					$tmp['form'] = "<input type = 'text' class = 'input w".$tmp['width']."' id = '".$k."' name = '$k' ".$v['edit']." value = '".$v['data']."'>\n";
					$tmp['js'] .= "<script>$(function() {";
					$tmp['js'] .= "$( '#".$k."' ).datepicker({dateFormat: 'yy-mm-dd'});";
					$tmp['js'] .= "}); </script>\n";
					break;

				case "datetime" : 
					$tmp['form'] = "<input type = 'text' class = 'input w".$tmp['width']."' id = '".$k."' name = '$k' ".$v['edit']." value = '".$v['data']."'>\n";
					$tmp['js'] .= "<script>$(function() {";
					$tmp['js'] .= "$( '#".$k."' ).datetimepicker({dateFormat: 'yy-mm-dd'});";
					$tmp['js'] .= "}); </script>\n";
					break;

				case "time" : 
					$tmp['form'] = "<input type = 'text' class = 'input w".$tmp['width']."' id = '".$k."' name = '$k' ".$v['edit']." value = '".$v['data']."'>\n";
					$tmp['js'] .= "<script>$(function() {";
					$tmp['js'] .= "$( '#".$k."' ).timepicker();";
					$tmp['js'] .= "}); </script>\n";
					break;

				case "select" :
					$tmp['form'] = "<select name = '$k'>\n";

					if( isset($v['set']) ){
						$compare = ($v['data'])?$v['data']:$this->data[$k];

						foreach($v['set'] as $i => $str){
							if($compare == $i){
								$tmp['form'] .= "<option value = '".$i."' selected>".$str."</option>\n";
							}else{
								$tmp['form'] .= "<option value = '".$i."'>".$str."</option>\n";
							}
						}
					}

					$tmp['form'] .= "</select>\n";
					break;

				case "radio" :

					if( isset($v['set']) ){
						$compare = ($v['data'])?$v['data']:$this->data[$k];

						foreach($v['set'] as $i => $str){
							if($compare == $i){
								$tmp['form'] .= "<input type = 'radio' name = '$k' value = '".$i."' id = '$k$i' checked><label class = 'radio' for='$k$i'>".$str."</label>\n";
							}else{
								$tmp['form'] .= "<input type = 'radio' name = '$k' value = '".$i."' id = '$k$i'><label class = 'radio' for = '$k$i'>".$str."</label>\n";
							}
						}
					}

					break;

				case "checkbox" :

					if( isset($v['set']) ){
						if(isset($v['data'])){
							$data = explode(",", $v['data']);
						}else{
							$data = explode(",", $this->data[$k]);
						}

						foreach($v['set'] as $i => $str){
							$check = false;

							foreach($data as $chk => $chv){
								if($chv == $i){
									$check = true;
								}
							}

							if($check == true){
								$tmp['form'] .= "<input type = 'checkbox' name = '".$k."[]' value = '".$i."' id = '$k$i' checked><label class = 'radio' for='$k$i'>".$str."</label>\n";
							}else{
								$tmp['form'] .= "<input type = 'checkbox' name = '".$k."[]' value = '".$i."' id = '$k$i'><label class = 'radio' for = '$k$i'>".$str."</label>\n";
							}
						}
					}

					break;

				default : $tmp['form'] = "<input type = 'text' id = '$k' class = 'input w".$tmp['width']."' name = '$k' ".$v['edit']." value = '".$v['data']."'>\n";
					break;
			}

			if(isset($v['require'])){
				switch($v['require']){
					case "text" :
						$tmp['form'] = str_replace("class = 'input","class = 'input validate[required] text-input", $tmp['form']);
						break;
					case "number" :
						$tmp['form'] = str_replace("class = 'input","class = 'input validate[required,custom[onlyLetterNumber]]", $tmp['form']);
						break;
					case "email" :
						$tmp['form'] = str_replace("class = 'input","class = 'input validate[required,custom[email]]", $tmp['form']);
						break;
				}
				$tmp['form'] = $tmp['form']. "<span class='require'>*</span>";
			}

			if(isset($v['lang'])){
				$langs = explode("/", $v['lang']);
				$tmp_form = $tmp['form'];
				foreach($langs as $lang){
					$tmp['form'] = preg_replace('/name = \'(\w+)\'/i', 'name = \'${1}_'.$lang.'\'', $tmp_form);
					$tmp['form'] = preg_replace('/id = \'(\w+)\'/i', 'id = \'${1}_'.$lang.'\'', $tmp['form']);
					$tmp['key'] = $k."_".$lang;
					$tmp['lang'] = $lang;
					$tmp['flag'] = "<img src='".URL."admin/html/images/".$lang.".gif'>";
					array_push($this->row, $tmp);
				}
			}else{
				array_push($this->row, $tmp);
			}
		}
	}

	function compile_data(){

		foreach($this->row as $key => $value){

			if($this->attr[$value['key']]['data']){
				// make hidden value for real value
				 $this->row[$key]['hidden'] = "<input type = 'hidden' name = '_".$key."'>\n";

				// replace with setting value of use call_back_function
				if(strstr($this->attr[$value['key']]['data'], "function")){

					$function_name = trim(str_replace("function", "", $this->attr[$value['key']]['data']));
					$function_name = str_replace("()", "", $function_name);

					if ( !function_exists('$function_name') ) :
						$this->data[$value['key']] = call_user_func($function_name);
					endif;

				}else{
					$this->data[$value['key']] = $this->attr[$value['key']]['data'];
				}
			}

			switch ($value['type']) {
				case "text" : 
					$this->row[$key]['form'] = str_replace("value = ''", " value = '".$this->data[$value['key']]."'", $value['form']);
					break;

				case "upload" :
					if($value['show'] == true){
						$this->row[$key]['form'] = str_replace("<", "<img src = '".$this->data[$value['key']]."'><br/><", $value['form']);
					}
					break;

				case "textarea" :
					$this->row[$key]['form'] = str_replace("><", ">".$this->data[$value['key']]."<", $value['form']);
					break;

				case "hidden" :
					$this->row[$key]['form'] = str_replace("value = ''", " value = '".$this->data[$value['key']]."'", $value['form']);
					break;

				case "date" :
					$this->row[$key]['form'] = str_replace("value = ''", " value = '".$this->data[$value['key']]."'", $value['form']);
					break;

				case "datetime" :
					$this->row[$key]['form'] = str_replace("value = ''", " value = '".$this->data[$value['key']]."'", $value['form']);
					break;

				case "time" :
					$this->row[$key]['form'] = str_replace("value = ''", " value = '".$this->data[$value['key']]."'", $value['form']);
					break;

				default :
					$this->row[$key]['form'] = str_replace("value = ''", " value = '".$this->data[$value['key']]."'", $value['form']);
					break;
			}
		}
	}

	function add_action($act){
		global $dsfm_module;
		$this->action = URL . "admin/".$dsfm_module."/".$act;
	}

	function compile(){
		$this->compile_form();

		if(count($this->data)){
			$this->compile_data();
		}
	}

	function rain_display($tpl, $template = "form"){

		$this->compile();

		$tpl->assign("row", $this->row);
		$tpl->assign("action", $this->action);
		$tpl->draw($template);
	}
}

?>